import type { RankedInfoFormat } from '@/types/ranked'
import { getRankedStatsApi } from '@/api/client/userApi'
import i18n from '@/i18n'

export async function getRankInfo(puuid: string) {
  const rankData: RankedInfoFormat = {}
  const res = await getRankedStatsApi(puuid)
  const { RANKED_SOLO_5x5: solo, RANKED_FLEX_SR: flex } = res?.queueMap

  // 单双排
  if (solo) {
    const soloText =
      solo.division && solo.division !== 'NA'
        ? `${i18n.global.t(`Tiers.${solo.tier || 'UNRANKED'}`)} ${solo.division}`
        : `${i18n.global.t(`Tiers.${solo.tier || 'UNRANKED'}`)}`
    const soloAbbrText = i18n.global.t(`shortTiers.${solo.tier || 'UNRANKED'}`)
    const solohighestText =
      solo.highestDivision && solo.highestDivision !== 'NA'
        ? `${i18n.global.t(`Tiers.${solo.highestTier || 'UNRANKED'}`)} ${solo.highestDivision}`
        : `${i18n.global.t(`Tiers.${solo.highestTier || 'UNRANKED'}`)}`

    rankData.solo = {
      text: soloText,
      abbrText: soloAbbrText,
      highestText: solohighestText,
      wins: solo.wins,
      tier: solo.tier || 'UNRANKED',
      division: solo.division,
      lp: solo.leaguePoints,
    }
  }

  // 组排
  if (flex) {
    const flexText =
      flex.division && flex.division !== 'NA'
        ? `${i18n.global.t(`Tiers.${flex.tier || 'UNRANKED'}`)} ${flex.division}`
        : `${i18n.global.t(`Tiers.${flex.tier || 'UNRANKED'}`)}`
    const flexAbbrText = i18n.global.t(`shortTiers.${flex.tier || 'UNRANKED'}`)
    const flexhighestText =
      flex.highestDivision && flex.highestDivision !== 'NA'
        ? `${i18n.global.t(`Tiers.${flex.highestTier || 'UNRANKED'}`)} ${flex.highestDivision}`
        : `${i18n.global.t(`Tiers.${flex.highestTier || 'UNRANKED'}`)}`

    rankData.flex = {
      text: flexText,
      abbrText: flexAbbrText,
      highestText: flexhighestText,
      wins: flex.wins,
      tier: flex.tier || 'UNRANKED',
      division: flex.division,
      lp: flex.leaguePoints,
    }
  }

  return rankData
}
